﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PO_2011;

namespace WindowsFormsApplication1
{
    public partial class MorfologiaMatemetyczna : Form
    {
        private Pnm p;
        private int MacierzWidthIHight = 7, TypMorfologii=1;
        public Form1 refToMainWindow;
        public Bitmap oryginalB;

        public MorfologiaMatemetyczna(Pnm objectPnm)
        {
            InitializeComponent();
            p = objectPnm; oryginalB = new Bitmap(p.getBitmap());
            pictureBox1.Size = new System.Drawing.Size(p.GetWidth, p.GetHeight);
            this.ClientSize = new System.Drawing.Size(p.GetWidth + 550, p.GetHeight + 280);
            pictureBox1.Image = p.getBitmap(); comboBox1.Text = "7x7"; comboBox2.Text = "Dylacja";
            checkBox25.BackColor = Color.Black;
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string s = comboBox1.SelectedItem.ToString();
            if (s == "3x3")
            {
                MacierzWidthIHight = 3;
                checkBox9.Enabled = false;
                checkBox10.Enabled = false;
                checkBox11.Enabled = false;
                checkBox12.Enabled = false;
                checkBox13.Enabled = false;
                checkBox41.Enabled = false;
                checkBox37.Enabled = false;
                checkBox40.Enabled = false;
                checkBox39.Enabled = false;
                checkBox38.Enabled = false;
                checkBox37.Enabled = false;
                checkBox20.Enabled = false;
                checkBox27.Enabled = false;
                checkBox34.Enabled = false;
                checkBox16.Enabled = false;
                checkBox23.Enabled = false;
                checkBox30.Enabled = false;

                checkBox1.Enabled = false;
                checkBox2.Enabled = false;
                checkBox3.Enabled = false;
                checkBox4.Enabled = false;
                checkBox5.Enabled = false;
                checkBox6.Enabled = false;
                checkBox7.Enabled = false;
                checkBox14.Enabled = false;
                checkBox21.Enabled = false;
                checkBox28.Enabled = false;
                checkBox35.Enabled = false;
                checkBox42.Enabled = false;
                checkBox49.Enabled = false;
                checkBox42.Enabled = false;
                checkBox43.Enabled = false;
                checkBox44.Enabled = false;
                checkBox45.Enabled = false;
                checkBox46.Enabled = false;
                checkBox47.Enabled = false;
                checkBox48.Enabled = false;
                checkBox36.Enabled = false;
                checkBox29.Enabled = false;
                checkBox22.Enabled = false;
                checkBox15.Enabled = false;
                checkBox8.Enabled = false;
            }
            else
                if (s == "7x7")
                {
                    MacierzWidthIHight = 7;
                    checkBox9.Enabled = true;
                    checkBox10.Enabled = true;
                    checkBox11.Enabled = true;
                    checkBox12.Enabled = true;
                    checkBox13.Enabled = true;
                    checkBox41.Enabled = true;
                    checkBox37.Enabled = true;
                    checkBox40.Enabled = true;
                    checkBox39.Enabled = true;
                    checkBox38.Enabled = true;
                    checkBox37.Enabled = true;
                    checkBox20.Enabled = true;
                    checkBox27.Enabled = true;
                    checkBox34.Enabled = true;
                    checkBox16.Enabled = true;
                    checkBox23.Enabled = true;
                    checkBox30.Enabled = true;

                    checkBox1.Enabled = true;
                    checkBox2.Enabled = true;
                    checkBox3.Enabled = true;
                    checkBox4.Enabled = true;
                    checkBox5.Enabled = true;
                    checkBox6.Enabled = true;
                    checkBox7.Enabled = true;
                    checkBox14.Enabled = true;
                    checkBox21.Enabled = true;
                    checkBox28.Enabled = true;
                    checkBox35.Enabled = true;
                    checkBox42.Enabled = true;
                    checkBox49.Enabled = true;
                    checkBox42.Enabled = true;
                    checkBox43.Enabled = true;
                    checkBox44.Enabled = true;
                    checkBox45.Enabled = true;
                    checkBox46.Enabled = true;
                    checkBox47.Enabled = true;
                    checkBox48.Enabled = true;
                    checkBox36.Enabled = true;
                    checkBox29.Enabled = true;
                    checkBox22.Enabled = true;
                    checkBox15.Enabled = true;
                    checkBox8.Enabled = true;
                }
                else
                {
                    MacierzWidthIHight = 5;
                    checkBox1.Enabled = false;
                    checkBox2.Enabled = false;
                    checkBox3.Enabled = false;
                    checkBox4.Enabled = false;
                    checkBox5.Enabled = false;
                    checkBox6.Enabled = false;
                    checkBox7.Enabled = false;
                    checkBox14.Enabled = false;
                    checkBox21.Enabled = false;
                    checkBox28.Enabled = false;
                    checkBox35.Enabled = false;
                    checkBox42.Enabled = false;
                    checkBox49.Enabled = false;
                    checkBox42.Enabled = false;
                    checkBox43.Enabled = false;
                    checkBox44.Enabled = false;
                    checkBox45.Enabled = false;
                    checkBox46.Enabled = false;
                    checkBox47.Enabled = false;
                    checkBox48.Enabled = false;
                    checkBox36.Enabled = false;
                    checkBox29.Enabled = false;
                    checkBox22.Enabled = false;
                    checkBox15.Enabled = false;
                    checkBox8.Enabled = false;
                }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Double[,] Macierz = new Double[MacierzWidthIHight, MacierzWidthIHight];

            if (MacierzWidthIHight == 3)
            {
                for (int i = 0; i < 3; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Macierz[i, j] = 0;
                    }
                }

                if (checkBox17.Checked == true) Macierz[0, 2] = 1;
                if (checkBox18.Checked == true) Macierz[0, 1] = 1;
                if (checkBox19.Checked == true) Macierz[0, 0] = 1;
                if (checkBox24.Checked == true) Macierz[1, 2] = 1;
                if (checkBox25.Checked == true) Macierz[1, 1] = 1;
                if (checkBox26.Checked == true) Macierz[1, 0] = 1;
                if (checkBox31.Checked == true) Macierz[2, 2] = 1;
                if (checkBox32.Checked == true) Macierz[2, 1] = 1;
                if (checkBox33.Checked == true) Macierz[2, 0] = 1;




            }

            if (MacierzWidthIHight == 5)
            {
                for (int i = 0; i < 5; i++)
                {
                    for (int j = 0; j < 5; j++)
                    {
                        Macierz[i, j] = 0;
                    }
                }

                if (checkBox9.Checked == true) Macierz[0, 4] = 1;
                if (checkBox10.Checked == true) Macierz[0, 3] = 1;
                if (checkBox11.Checked == true) Macierz[0, 2] = 1;
                if (checkBox12.Checked == true) Macierz[0, 1] = 1;
                if (checkBox13.Checked == true) Macierz[0, 0] = 1;

                if (checkBox16.Checked == true) Macierz[1, 4] = 1;
                if (checkBox17.Checked == true) Macierz[1, 3] = 1;
                if (checkBox18.Checked == true) Macierz[1, 2] = 1;
                if (checkBox19.Checked == true) Macierz[1, 1] = 1;
                if (checkBox20.Checked == true) Macierz[1, 0] = 1;

                if (checkBox23.Checked == true) Macierz[2, 4] = 1;
                if (checkBox24.Checked == true) Macierz[2, 3] = 1;
                if (checkBox25.Checked == true) Macierz[2, 2] = 1;
                if (checkBox26.Checked == true) Macierz[2, 1] = 1;
                if (checkBox27.Checked == true) Macierz[2, 0] = 1;

                if (checkBox30.Checked == true) Macierz[3, 4] = 1;
                if (checkBox31.Checked == true) Macierz[3, 3] = 1;
                if (checkBox32.Checked == true) Macierz[3, 2] = 1;
                if (checkBox33.Checked == true) Macierz[3, 1] = 1;
                if (checkBox34.Checked == true) Macierz[3, 0] = 1;

                
                if (checkBox37.Checked == true) Macierz[4, 5] = 1;
                if (checkBox38.Checked == true) Macierz[4, 4] = 1;
                if (checkBox39.Checked == true) Macierz[4, 3] = 1;
                if (checkBox40.Checked == true) Macierz[4, 2] = 1;
                if (checkBox41.Checked == true) Macierz[4, 1] = 1;
                

            }

            if (MacierzWidthIHight == 7)
            {
                
                for (int i = 0; i < 7; i++)
                {
                    for (int j = 0; j < 7; j++)
                    {
                        Macierz[i, j] = 0;
                    }
                }

                if (checkBox1.Checked == true) Macierz[0, 0] = 1;
                if (checkBox2.Checked == true) Macierz[0, 1] = 1;
                if (checkBox3.Checked == true) Macierz[0, 2] = 1;
                if (checkBox4.Checked == true) Macierz[0, 3] = 1;
                if (checkBox5.Checked == true) Macierz[0, 4] = 1;
                if (checkBox6.Checked == true) Macierz[0, 5] = 1;
                if (checkBox7.Checked == true) Macierz[0, 6] = 1;

                if (checkBox8.Checked == true) Macierz[1, 6] = 1;
                if (checkBox9.Checked == true) Macierz[1, 5] = 1;
                if (checkBox10.Checked == true) Macierz[1, 4] = 1;
                if (checkBox11.Checked == true) Macierz[1, 3] = 1;
                if (checkBox12.Checked == true) Macierz[1, 2] = 1;
                if (checkBox13.Checked == true) Macierz[1, 1] = 1;
                if (checkBox14.Checked == true) Macierz[1, 0] = 1;

                if (checkBox15.Checked == true) Macierz[2, 6] = 1;
                if (checkBox16.Checked == true) Macierz[2, 5] = 1;
                if (checkBox17.Checked == true) Macierz[2, 4] = 1;
                if (checkBox18.Checked == true) Macierz[2, 3] = 1;
                if (checkBox19.Checked == true) Macierz[2, 2] = 1;
                if (checkBox20.Checked == true) Macierz[2, 1] = 1;
                if (checkBox21.Checked == true) Macierz[2, 0] = 1;

                if (checkBox22.Checked == true) Macierz[3, 6] = 1;
                if (checkBox23.Checked == true) Macierz[3, 5] = 1;
                if (checkBox24.Checked == true) Macierz[3, 4] = 1;
                if (checkBox25.Checked == true) Macierz[3, 3] = 1;
                if (checkBox26.Checked == true) Macierz[3, 2] = 1;
                if (checkBox27.Checked == true) Macierz[3, 1] = 1;
                if (checkBox28.Checked == true) Macierz[3, 0] = 1;

                if (checkBox29.Checked == true) Macierz[4, 6] = 1;
                if (checkBox30.Checked == true) Macierz[4, 5] = 1;
                if (checkBox31.Checked == true) Macierz[4, 4] = 1;
                if (checkBox32.Checked == true) Macierz[4, 3] = 1;
                if (checkBox33.Checked == true) Macierz[4, 2] = 1;
                if (checkBox34.Checked == true) Macierz[4, 1] = 1;
                if (checkBox35.Checked == true) Macierz[4, 1] = 1;

                if (checkBox36.Checked == true) Macierz[5, 6] = 1;
                if (checkBox37.Checked == true) Macierz[5, 5] = 1;
                if (checkBox38.Checked == true) Macierz[5, 4] = 1;
                if (checkBox39.Checked == true) Macierz[5, 3] = 1;
                if (checkBox40.Checked == true) Macierz[5, 2] = 1;
                if (checkBox41.Checked == true) Macierz[5, 1] = 1;
                if (checkBox42.Checked == true) Macierz[5, 0] = 1;

                if (checkBox43.Checked == true) Macierz[6, 6] = 1;
                if (checkBox44.Checked == true) Macierz[6, 5] = 1;
                if (checkBox45.Checked == true) Macierz[6, 4] = 1;
                if (checkBox46.Checked == true) Macierz[6, 3] = 1;
                if (checkBox47.Checked == true) Macierz[6, 2] = 1;
                if (checkBox48.Checked == true) Macierz[6, 1] = 1;
                if (checkBox49.Checked == true) Macierz[6, 0] = 1;
            }
            p.bitmapOryginal = oryginalB;
            pictureBox1.Image = p.MorfologiaMatematyczna(MacierzWidthIHight, TypMorfologii,Macierz);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.refToMainWindow.pictureBox1.Image = p.BitmapFiltr;
            p.bitmap = p.BitmapFiltr;
            this.Close();
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
             string s = comboBox2.SelectedItem.ToString();
             if (s == "Dylacja")
            {
                TypMorfologii = 1;
            }
            else
                 if (s == "Erozja")
                {
                    TypMorfologii = 2;
                }
                 else
                     if (s == "Otwarcie")
                {
                    TypMorfologii = 3;
                }
                     else TypMorfologii = 4;
        }

      

    }
}
